package com.goldplusgold.network.lib;

import android.content.Context;
import android.util.Log;

import com.zhy.http.okhttp.OkHttpUtils;

import java.util.Map;

import okhttp3.OkHttpClient;

public class NetworkRequestsUtil implements INetworkRequests {
    private static final String TAG = "post";
    private static final long CONNECT_TIME_OUT = 10 * 1000L;
    private static final long READ_TIME_OUT = 10 * 1000L;
    public static OkHttpClient okHttpClient;
    private static NetworkRequestsUtil networkRequestsUtil;

    public static NetworkRequestsUtil getInstance() {
        if (networkRequestsUtil == null) {
            networkRequestsUtil = new NetworkRequestsUtil();
        }
        return networkRequestsUtil;
    }


    public void defaultHeaderParams(Map<String, String> headerMap) {
        if (headerMap != null) {
            Log.e("xsy", "headerMap" + headerMap);
        }

    }

    public void defaultBodyParams(Map<String, String> paramsMap) {
        if (paramsMap != null) {
            Log.e("xsy", "paramsMap data : " + paramsMap.toString());
        }
    }

    public void defaultParams(Map<String, String> headerMap, Map<String, String> paramsMap) {
        defaultHeaderParams(headerMap);
        defaultBodyParams(paramsMap);
    }


    @Override
    public void stopNetworkRequests() {
        OkHttpUtils.getInstance().cancelTag(TAG);
        Log.e("xsy", "stopNetworkRequests");
    }

    @Override
    public void startNetworkRequests(final String url, final Map<String, String> headersMap, final Map<String, String> paramsMap, final ResponseListener listener, Context context) {
        defaultParams(headersMap, paramsMap);
        OkHttpUtils.post()
                .url(url)
                .params(paramsMap)
                .headers(headersMap)
                .tag("post")
                .build()
                .execute(new HttpHandler(listener, context, url));
    }

    /**
     * get 请求
     *
     * @param url
     */
    public void startNetworkRequests(final String url, final ResponseListener listener, Context context) {

        Log.e("xsy", "startNetworkRequests");
        Log.e("xsy", "url " + url);
        OkHttpUtils.get()
                .url(url)
                .tag("get")
                .build()
                .execute(new HttpHandler(listener, context, url));
    }


    @Override
    public void startAsynchronousRequests(final String url, final Map<String, String> headersMap, final Map<String, String> paramsMap, final ResponseListener listener, Context context) {
        defaultParams(headersMap, paramsMap);
        Log.e("xsy", "startAsynchronousRequests: url " + url);
        OkHttpUtils.post()
                .url(url)
                .params(paramsMap)
                .headers(headersMap)
                .build()
                .execute(new HttpHandler(listener, context, url));
    }

    @Override
    public void startAsynchronousRequestNotEncryptAES(final String url, final Map<String, String> headersMap, final Map<String, String> paramsMap, final ResponseListener listener, Context context) {
        Log.e("xsy", "startAsynchronousRequests: url " + url);
        OkHttpUtils.post()
                .url(url)
                .params(paramsMap)
                .headers(headersMap)
                .build()
                .execute(new HttpHandler(listener, context, url));
    }
}
